package com.cloud.lan.api;

import com.cloud.lan.api.dto.req.BaseReq;
import com.cloud.lan.api.dto.req.LanUserReq;
import com.cloud.lan.api.dto.req.WxLoginRequest;
import com.cloud.lan.api.dto.resp.BaseUserInfoResp;
import com.cloud.lan.api.dto.resp.OnLoginResp;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.enums.RedisKeyEnum;
import com.cloud.lan.service.ILanUserService;
import com.cloud.lan.service.impl.RedisService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import cn.dustlight.captcha.annotations.CodeValue;
import cn.dustlight.captcha.annotations.SendCode;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户信息入口
 * @author ErKang
 * @date Created in 2021/12/5 04:24
 * @title TestApi.java
 * Department: Product development
 */
@Slf4j
@RestController
@RequestMapping(value = {"/v1/user/api/"}, method = {RequestMethod.GET, RequestMethod.POST}, consumes = {"*/*"}, produces = {"application/json"})
public class LanUserApi {

    @Resource
    ILanUserService lanUserService;
    @Resource
    RedisService redisService;

    @RequestMapping("onLogin")
    public BaseResponse<OnLoginResp> onLogin(WxLoginRequest request) {
        return lanUserService.onLogin(request);
    }

    @RequestMapping("onUserInfo")
    public BaseResponse<Object> onUserInfo(@RequestHeader(BaseReq.ut) String token, WxLoginRequest request) {
        request.setToken(token);
        return lanUserService.onUserInfo(request);
    }

    @RequestMapping("authLogin")
    public BaseResponse<Object> authLogin(@RequestHeader(BaseReq.ut) String token, @RequestBody LanUserReq request) {
        request.setToken(token);
        return lanUserService.authLogin(request);
    }

    @RequestMapping("get")
    public BaseResponse<Object> get(@RequestHeader(BaseReq.ut) String token) {
        return lanUserService.get(token);
    }

    @RequestMapping("add")
    public BaseResponse<Object> add(@RequestBody LanUserReq request) {
        return lanUserService.add(request);
    }

    @RequestMapping("update")
    public BaseResponse<Object> update(@RequestHeader(BaseReq.ut) String token, @RequestBody LanUserReq request) {
        request.setToken(token);
        return lanUserService.update(request);
    }

    @GetMapping("getSmsCode")
    public BaseResponse<Object> getSmsCode(@RequestHeader(BaseReq.ut) String token, LanUserReq request) {
        request.setToken(token);
        return lanUserService.getSmsCode(request);
    }

    @RequestMapping("getJsonObj")
    public String getJsonObj(String method) {
        return lanUserService.getJsonObj(method);
    }

    @SendCode
    @RequestMapping("/getImgCode")
    public void captcha(@CodeValue String imgCode, String token) {
        // 在此处进行自定义的业务，验证码的生成、发送与储存已由注解 '@SendCode' 完成。
        String key = token.replaceAll(" ", "");
        redisService.setEx(RedisKeyEnum.IMG_CODE.key(key), 300, imgCode);
        String code = redisService.get(RedisKeyEnum.IMG_CODE.key(key));
        log.info("生成的验证码:{} 存入的key是： {} 存入缓存code是： {}", imgCode, RedisKeyEnum.IMG_CODE.key(key), code);
    }

//    @GetMapping("getImgCode")
//    public void getImgCode(HttpServletResponse response, String token) {
//        //设置response响应
//        response.setDateHeader("Expires", 0L);
//        response.setHeader("Pragma", "No-cache");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("image/jpeg");
//        //定义图形验证码的长和宽
//        ShearCaptcha lineCaptcha = CaptchaUtil.createShearCaptcha(200, 50, 4, 4);
//        //把凭证对应的验证码信息保存到reids（可从redis中获取）
//        // redis有效期300秒
//        redis.setex(RedisKeyEnum.IMG_CODE.key(token), 300, lineCaptcha.getCode());
//        try {
//            // 输出到页面
//            lineCaptcha.write(response.getOutputStream());
//            // 打印日志
//            String code = redis.get(RedisKeyEnum.IMG_CODE.key(token));
//            log.info("生成的验证码:{} 存入缓存code是： {}", lineCaptcha.getCode(), code);
//        } catch (Exception e) {
//            log.error("生成验证码失败: ", e);
//        } finally {
//            // 关闭流
//            try {
//                response.getOutputStream().close();
//            } catch (IOException e) {
//                log.error("流关闭错误: ", e);
//            }
//        }
//    }

    @GetMapping("baseUserInfo")
    public BaseResponse<BaseUserInfoResp> baseUserInfo(@RequestHeader(BaseReq.ut) String token, LanUserReq request) {
        request.setToken(token);
        return lanUserService.baseUserInfo(request);
    }

}
